# X-Plane plugin for PoKeys devices - Readme and specifications
#  Author: Matev Bonak
#  Changelog: 
#  - initial release
#  - 11.6.2014: Added the support for custom datarefs, analog inputs and PWM outputs
#  - 15.2.2015: Logic comparison option included at the end of the line for digital outptus
#  - 31.3.2015: Added support for encoders, search for ethernet devices always enabled
#  - 27.11.2015: Added the support for PoIL shared data slots
#  - 25.5.2016: Fixed mapping to and from vector data
#  - 24.6.2018: Fixed matrix LED 7-segment display mapping, fixed comparison operations, fixed writing to vector variables
#  - 14.9.2018: Fixed digits rendering for matrix LED displays
#
# Description:
# 
# This manual provides user with the basic information on using and setting up the X-Plane plugin for PoKeys devices.
# The plugin supports the following operations:
# - mapping of digital inputs to variables
# - mapping of separate digital input values to variables
# - mapping of variables to digital outputs
# - mapping of variables to PoExtBus outputs
# - displaying variables on the LCD
# - displaying variables on 7-segment LED display
#
# * The plugin supports PoKeys55, PoKeys56U, PoKeys56E, PoKeys57U and PoKeys57E devices.
#
#
# Usage
#
# This file MUST BE PLACED IN THE X-PLANE APPLICATION FOLDER (where X-Plane.exe is found).
# Folder with the plugin must be placed in the Resources/plugins folder.
# All lines starting with # sign are comments and are ignored by the plugin.
#
# This file can contains settings for multiple PoKeys devices. 
# Each device is identified by a serial number in [ ] brackets and each line contains one specification. 
#
# The following mappings are supported:
#
#** X-Plane variable to digital output mapping
#     Value of the specified variable is read and compared with the threshold value. If the value is greater than the threshold, the output is activated.
#     Parameters:
#      - Pin board ID: ID of the pin as labelled on the PoKeys card
#      - OUT: direction of the pin - must be set to OUT to enable digital output
#      - inverted/non-inverted: if this parameter is set to 1, digital output state is inverted
#      - key code and modifier: not used, can be left empty
#      - variable: X-Plane variable name as specified in DataRefs.txt - if variable is exported by one of the plugins during initialization, add ! before it.
#      - variable type: variable type as specified in DataRefs.txt.
#            i: integer value
#            f: float value
#            vi: vector of integers
#            vf: vector of floats
#      - variable index: index of the value in the variable vector, must be 0 if variable type is integer or float (no vector)
#      - threshold value: threshold value that the variable gets compared to in order to activate the output
#     (optional) comparison type: EQ, GT (default), GTE, LT, LTE, NEQ
#
#   Format: Pin{Pin board ID}={OUT},{0-non/1-inverted},{key code and modifier},{variable},{variable type - i, f, vi, vf},{variable index},{minimum value for activating output or multiplier value for the inputs} 
#   Example: Pin1=OUT,0,,sim/cockpit2/engine/actuators/throttle_ratio_all,f,0,0.5
#   Description: set 'pin 1' as 'digital output' that reflects whether the 'float' variable named 'sim/cockpit2/engine/actuators/throttle_ratio_all' is greater than '0.5'
#
#
#
#** digital input to X-Plane variable mapping
#     Value of the digital input is read, multiplied by the multiplier value and written to the specified variable. If the switch is connected between PoKeys pin and GND, digital inputs gets value of 1 when the switch is closed
#     Parameters:
#      - Pin board ID: ID of the pin as labelled on the PoKeys card
#      - IN: direction of the pin - must be set to IN to enable digital input
#      - inverted/non-inverted: if this parameter is set to 1, digital output state is inverted
#      - key code and modifier: not used, can be left empty
#      - variable: X-Plane variable name as specified in DataRefs.txt
#      - variable type: variable type as specified in DataRefs.txt.
#            i: integer value
#            f: float value
#            vi: vector of integers
#            vf: vector of floats
#      - variable index: index of the value in the variable vector, must be 0 if variable type is integer or float (no vector)
#      - multiplier value: multiplier value that the input gets mutliplied by
#
#   Format: Pin{Pin board ID}={IN},{0-non/1-inverted},{key code and modifier},{variable},{variable type - i, f, vi, vf},{variable index},{mutliplier} 
#   Example: Pin4=IN,0,,sim/cockpit/switches/no_smoking,i,0,1
#   Description: set 'pin 4' as 'digital input' whos state is written in the 'integer' variable named 'sim/cockpit2/engine/actuators/throttle_ratio_all' with the value of '1' when the switch is activated and 0 otherwise
#
#
#
#
#** separate digital input value mapping to X-Plane variable 
#     The value of the digital input is read and if the input is activated in the specified sense, the multiplier value is written to the specified variable. If the switch is connected between PoKeys pin and GND, digital inputs gets value of 1 when the switch is closed. This mapping type can be used with multiple selection switches where the switches are exclusive (only one can be activated at a time) and are all mapped to the same variable name
#     Parameters:
#      - Pin board ID: ID of the pin as labelled on the PoKeys card
#      - IN_1/IN_0: digital input activation condition. If IN_1 is specified, the multiplier value is written only when the switch is pushed. When released, no action is taken. If IN_0 is specified, no action is taken if the switch is pushed and the multiplier is written to the variable value when the switch is released.
#      - inverted/non-inverted: if this parameter is set to 1, digital output state is inverted
#      - key code and modifier: not used, can be left empty
#      - variable: X-Plane variable name as specified in DataRefs.txt
#      - variable type: variable type as specified in DataRefs.txt.
#            i: integer value
#            f: float value
#            vi: vector of integers
#            vf: vector of floats
#      - variable index: index of the value in the variable vector, must be 0 if variable type is integer or float (no vector)
#      - multiplier value: multiplier value that the input gets mutliplied by
#
#   Format: Pin{Pin board ID}={IN_1/IN_0},{0-non/1-inverted},{key code and modifier},{variable},{variable type - i, f, vi, vf},{variable index},{mutliplier} 
#   Example: Pin4=IN_1,0,,sim/cockpit/switches/no_smoking,i,0,1
#   Description: set 'pin 4' as 'digital input' whos state is written in the 'integer' variable named 'sim/cockpit2/engine/actuators/throttle_ratio_all' with the value of '1' ONLY when the switch is pushed
#
#
#
#
#
#** X-Plane variable to PoExtBus output mapping
#     Value of the specified variable is read and compared with the threshold value. If the value is greater than the threshold, the output is activated.
#     Parameters:
#      - PoExtBus output index: ID of the output. First PoExtBus card connected to the PoKeys have IDs 0 to 7, the second card 8 to 15, etc. Maximum ID value is 79.
#      - variable name, type, index: (same as for the digital outputs)
#      - threshold value: (same as for the digital outputs)
#     (optional) comparison type: EQ, GT (default), GTE, LT, LTE, NEQ
#
#   Format: Ext{PoExtBus output ID}={variable},{variable type - i, f, vi, vf},{variable index},{threshold}
#   Example: Ext0=sim/cockpit2/engine/actuators/throttle_ratio_all,f,0,0.1
#   Description: let PoExtBus output '0' reflect whether the 'float' variable named 'sim/cockpit2/engine/actuators/throttle_ratio_all' is greater than '0.1'
#
#** LCD commands
#     This enables displaying user specified text and values of the variables on the standard alphanumeric LCD display, connected to a PoKeys board
#     LCD support is activated with the LCD options command, for example:
#        LCD=1,4,20
#     This enables the 4x20 LCD on primary pins (see PoKeys manual). To define the contents of the LCD page, the following commands must be specified
#     The command structure is as follows:
#        LCD{row number}={row contents}
#     Row contents can be either a user text or a variable value, which must be specified in the following format:
#        {display specification|variable name|variable type|variable index|variable multiplier}
#        The display specification defines how the number is displayed - both how many characters the number will take and if it is zero-padded on the left. See the following examples for the value 3.141
#          ###0 produces    3
#          0.00 produces 3.14
#          00.0 produces 03.1
#          #0.0 produces  3.1
#          0000 produces 0003
#        # is used as a placeholder for a digit, while 0 will display a 0 instead of empty space
#
#    Example: LCD1=Throttle: {##0|sim/cockpit2/engine/actuators/throttle_ratio_all|f|0|100} %.
#    Description: This displays the following on the LCD screen (without quotes): "Throttle:  50 %" if the throttle variable value is at 0.5
#
#** Matrix LED (7-segment LED) commands
#     Matrix LED module must first be initialized by specifying number of digits per module
#       MatrixLED={number of digits on module 1},{number of digits on module 2}  
#     Then each digit of the display must be assigned a variable
#       MatrixLED{1/2}_Digit{1-8}={variable name},{variable type},{variable index},{multiplier},{digit offset},{_/0/-}
#         digit offset: the offset of the digit to be displayed (10 to the power of). Decimal point is automatically 
#                       turned on if the offset is set to 0. Decimal places right of the decimal point have the offsets less
#                       than 0, decimal places to the left have offsets larger than zero. For rounding the digit value, add 100 to the
#                       digit offset (-102 instead of -2)
#         _/0/- option: for decimal places to the left of the decimal point, additional options can be selected:
#                       _ makes a clear space when number is low enough and the digit is not needed to display
#                       0 forces zero instead of clear space for low numbers (zero-padding)
#                       - produces a minus sign if the number is negative
#     Example:  MatrixLED=8,5
#               MatrixLED1_Digit5=sim/cockpit/radios/nav1_stdby_freq_hz,i,0,0.01,-1,0
#     Description: Enable 8 digits on matrix LED outputs 1 and 5 outputs on matrix LED outputs 2. 
#                  Display '-1' digit (first digit to the right of the decimal point) of the integer variable
#                  'sim/cockpit/radios/nav1_stdby_freq_hz' that is multiplied by '0.01' (frequencies are provided in 10 Hz
#                  and in order to display the number in kHz, 0.01 multiplier is used).
#
#
#** PWM outputs
#   Configuration: One line per device must be present that sets the PWM period
#       PWM={period}
#         period is in counter ticks. If f is the target frequency, set the period to 25000000 / f
#         Example: 1kHz period -> PWM=25000
#
#    Each PWM output must be enabled on selected pins (PWM is available on pins 17-22)
#     Parameters:
#      - Pin board ID: ID of the pin as labelled on the PoKeys card
#      - variable: X-Plane variable name as specified in DataRefs.txt - value of 100 corresponds to 100% duty cycle
#      - variable type: variable type as specified in DataRefs.txt
#            i: integer value
#            f: float value
#            vi: vector of integers
#            vf: vector of floats
#      - variable index: index of the value in the variable vector, must be 0 if variable type is integer or float (no vector)
#      - multiplier value: multiplier value that the variable gets mutliplied by before handled as duty cycle
#
#   Format: Pin{Pin board ID}=PWM,,,{variable},{variable type - i, f, vi, vf},{variable index},{mutliplier}
#   Example: Pin17=PWM,,,sim/cockpit2/engine/actuators/throttle_ratio_all,f,0,5
#   Description: set 'pin 17' as PWM output whos state is written based of the 'float' variable named 'sim/cockpit2/engine/actuators/throttle_ratio_all'
#
#** Analog inputs
#    Each analog input must be enabled on selected pins (analog input is available on pins 41-47)
#     Parameters:
#      - Pin board ID: ID of the pin as labelled on the PoKeys card
#      - variable: X-Plane variable name as specified in DataRefs.txt
#      - variable type: variable type as specified in DataRefs.txt
#            i: integer value
#            f: float value
#            vi: vector of integers
#            vf: vector of floats
#      - variable index: index of the value in the variable vector, must be 0 if variable type is integer or float (no vector)
#      - multiplier value: value written to the variable when analog input is at maximum range (+3.3V)
#
#   Format: Pin{Pin board ID}=AN_IN,,,{variable},{variable type - i, f, vi, vf},{variable index},{mutliplier}
#   Example: Pin43=AN_IN,,,sim/cockpit2/engine/actuators/throttle_ratio_all,f,0,5
#   Description: set 'pin 43' as analog input whos state is written to the 'float' variable named 'sim/cockpit2/engine/actuators/throttle_ratio_all'
#
#** Encoders
#    Encoder must be configured with A and B pins. Optionally, reset pin can be configured that resets the variable to default value
#     Parameters:
#      - Encoder ID: ID of the encoder to configure - PoKeys devices support 25 encoders (ID between 1 and 25)
#      - Pin A: pin where encoder A signal is connected to
#      - Pin B: pin where encoder B signal is connected to
#      - Pin reset (optional): optional reset pin (always inverted)
#      - Step size: change of variable per each step of the encoder
#      - Fast step difference (optional): difference in encoder value between scans that will trigger fast changing
#      - Fast multiplier (optional): additional multiplier of the step size in fast mode
#      - Minimum value: minimum allowed value - if maximum value is lower than minimum value, roles of these two limits are inverted and wrap-around is used
#              If maximum value is set to 0 and minimum to 360, the values will wrap from 360 to 0...
#      - Maximum value: maximum allowed value
#      - Default value (optional): default value that is written to encoder on reset pin activation
#      - variable: X-Plane variable name as specified in DataRefs.txt
#      - variable type: variable type as specified in DataRefs.txt
#            i: integer value
#            f: float value
#            vi: vector of integers
#            vf: vector of floats
#      - variable index: index of the value in the variable vector, must be 0 if variable type is integer or float (no vector)
#
#   Format: Encoder{Encoder ID}={pin A},{pin B},{reset pin},{step},{fastStepDiff},{fastStepMult},{minValue},{maxValue},{defaultValue},{variable},{variable type - i, f, vi, vf},{variable index}
#   Example: Encoder1=10,11,12,0.01,10,5,0,1,0,sim/cockpit2/engine/actuators/throttle_ratio_all,f,0
#   Description: configure encoder 1 (pin 10 as A, pin 11 as B, pin 12 as reset) as encoder that changes the 'float' variable named 'sim/cockpit2/engine/actuators/throttle_ratio_all'
#
#** PoIL shared slot data
#    This feature allows the interchange of data between PoBlocks-based application, running in PoKeys device and X-Plane simulation
#     Parameters:
#      - Shared slot ID (ID between 0 and 63)
#      - variable: X-Plane variable name as specified in DataRefs.txt
#      - variable type: variable type as specified in DataRefs.txt
#            i: integer value
#            f: float value
#            vi: vector of integers
#            vf: vector of floats
#      - variable index: index of the value in the variable vector, must be 0 if variable type is integer or float (no vector)
#      - multiplier value: the value of the shared slot is multiplied with this value before being used by X-Plane
#      - direction: direction of data flow - can be either from X-Plane to PoIL share data (out) or from PoIL to X-Plane (in)
#
#   Format: PoIL{shared slot ID}={variable},{variable type - i, f, vi, vf},{variable index},{multiplier value},{direction}
#   Example: PoIL1=sim/cockpit2/engine/actuators/throttle_ratio_all,f,0,100,in
#   Description: use the value of PoIL shared data slot 1 to set the 'float' variable named 'sim/cockpit2/engine/actuators/throttle_ratio_all' to 1/100 of the slot value
[23456]
Pin1=OUT,0,,sim/cockpit2/engine/actuators/throttle_ratio_all,f,0,0.5,EQ   # only activate the output if variable is equal to 0.5
Pin2=OUT,0,,sim/cockpit2/annunciators/master_warning,i,0,0
Pin3=IN_1,0,,sim/cockpit/switches/no_smoking,i,0,0
Pin4=IN_1,0,,sim/cockpit/switches/no_smoking,i,0,1
Pin5=IN_1,0,,sim/cockpit/switches/no_smoking,i,0,2
Pin6=OUT,0,,sim/cockpit2/engine/actuators/throttle_ratio,vf,0,0.5
Ext0=sim/cockpit2/engine/actuators/throttle_ratio_all,f,0,0.1
Ext1=sim/cockpit2/engine/actuators/throttle_ratio_all,f,0,0.25
Ext2=sim/cockpit2/engine/actuators/throttle_ratio_all,f,0,0.37
Ext3=sim/cockpit2/engine/actuators/throttle_ratio_all,f,0,0.5
Ext4=sim/cockpit2/engine/actuators/throttle_ratio_all,f,0,0.62
Ext5=sim/cockpit2/engine/actuators/throttle_ratio_all,f,0,0.75
Ext6=sim/cockpit2/engine/actuators/throttle_ratio_all,f,0,0.88
Ext7=sim/cockpit2/engine/actuators/throttle_ratio_all,f,0,0.98,GTE        # only activate the output if variable is greater or equal to 0.5
LCD=1,4,20
LCD1=Throttle: {##0|sim/cockpit2/engine/actuators/throttle_ratio_all|f|0|100} %.
# Second device example for LED displays
[31049]
MatrixLED=8,8
MatrixLED1_Digit1=_
MatrixLED1_Digit2=sim/cockpit/radios/nav1_stdby_freq_hz,i,0,0.01,2,0
MatrixLED1_Digit3=sim/cockpit/radios/nav1_stdby_freq_hz,i,0,0.01,1,0
MatrixLED1_Digit4=sim/cockpit/radios/nav1_stdby_freq_hz,i,0,0.01,0,0
MatrixLED1_Digit5=sim/cockpit/radios/nav1_stdby_freq_hz,i,0,0.01,-1,0
MatrixLED1_Digit6=sim/cockpit/radios/nav1_stdby_freq_hz,i,0,0.01,-2,0
MatrixLED1_Digit7=sim/cockpit/radios/nav1_stdby_freq_hz,i,0,0.01,-3,0
MatrixLED1_Digit8=_


# PWM
PWM=25000
Pin17=PWM,,,sim/cockpit2/engine/actuators/throttle_ratio_all,f,0,5

# Analog inputs
#Pin43=AN_IN,,,sim/cockpit2/engine/actuators/throttle_ratio_all,f,0,5

# Encoders
Encoder1=10,11,12,0.01,10,5,0,1,0,sim/cockpit2/engine/actuators/throttle_ratio_all,f,0

# PoIL shared data slots
PoIL1=sim/cockpit2/engine/actuators/throttle_ratio_all,f,0,0.01,in
